package com.jjwxc.jwjskandriod.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFActivity;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.AllStackRequest;
import com.jjwxc.jwjskandriod.model.ChannelListResponse;
import com.jjwxc.jwjskandriod.model.TagListResopnse;
import com.jjwxc.jwjskandriod.readActivity.utils.UMConstant;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class OptionalPop {
    private BaseActivity mActivity;
    private String twoIds;
    private String threeIds;
    private String fourIds;
    private String fiveIds;
    private String sixIds;
    List<String> tagsId = new ArrayList<>();

    private TextView tv_tab_one;
    private TextView tv_tab_two;

    public void optionalPopWindow(View mView, Activity context, AllStackRequest value, PopOptionalListener listener) {

        View view = LayoutInflater.from(context).inflate(R.layout.optional_pop, null, false);
        mActivity = (BaseActivity) context;

        TagGroupView tgv_two = view.findViewById(R.id.tgv_two);
        TextView tv_two = view.findViewById(R.id.tv_two);

        TagGroupView tgv_three = view.findViewById(R.id.tgv_three);
        TextView tv_three = view.findViewById(R.id.tv_three);

        TagGroupView tgv_four = view.findViewById(R.id.tgv_four);
        TextView tv_four = view.findViewById(R.id.tv_four);

        TagGroupView tgv_five = view.findViewById(R.id.tgv_five);

        TagGroupView tgv_six = view.findViewById(R.id.tgv_six);
        TextView tv_ok = view.findViewById(R.id.tv_ok);
        TextView tv_quxiao = view.findViewById(R.id.tv_quxiao);
        LinearLayout ll_quxiao = view.findViewById(R.id.ll_quxiao);
        tv_tab_one = view.findViewById(R.id.tv_tab_one);
        tv_tab_two = view.findViewById(R.id.tv_tab_two);
        TextView tv_time = view.findViewById(R.id.tv_time);
        LinearLayout ll_time = view.findViewById(R.id.ll_time);
        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.PopupWindow);
        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1, context);
            }
        });

        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(mView, Gravity.BOTTOM, 0, 0);

        setBackgroundAlpha(0.3f, context);

        MobclickAgent.onEvent(context, UMConstant.PAGE_STACKS_MORE);

        ll_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        if (value != null) {
            if (value.getChannels() != null && value.getChannels().size() > 0) {
                twoIds = value.getChannels().get(0);
            }
            threeIds = value.getWordSize();
            fourIds = value.getPublishTime();
            tagsId = value.getTagIds();
            fiveIds = value.getReadingStatus();
            sixIds = value.getSortType();
        }
        if (value.getTagIds() != null && value.getTagIds().size() > 0) {
            tagsId = value.getTagIds();
            tv_tab_one.setBackgroundResource(R.drawable.tag_screen_select_false);
            tv_tab_one.setTextColor(Color.parseColor("#222222"));
            tv_tab_two.setText("已选" + tagsId.size() + "个");
            tv_tab_two.setBackgroundResource(R.drawable.item_tab_text_true);
            tv_tab_two.setTextColor(Color.parseColor("#ffffff"));
        } else {
            tv_tab_two.setBackgroundResource(R.drawable.tag_screen_select_false);
            tv_tab_two.setTextColor(Color.parseColor("#222222"));
            tv_tab_two.setText("筛选标签");
            tv_tab_one.setBackgroundResource(R.drawable.item_tab_text_true);
            tv_tab_one.setTextColor(Color.parseColor("#ffffff"));
        }

        List<String> list2 = new ArrayList<>();
        List<String> list2Ids = new ArrayList<>();
        String tab = value.getTab();
        if (StUtils.isEmpty(tab)) {
            tab = "";
        }
        ((FFActivity) context).post(Url.listChannel, null, ChannelListResponse.class, new FFNetWorkCallBack<ChannelListResponse>() {
            @Override
            public void onSuccess(ChannelListResponse response) {
                List<ChannelListResponse.DataBean> data = response.getData();
                if (response.getCode() == 200 && response.getData().size() > 0) {
                    int channelPos = -1;

                    for (int i = 0; i < data.size(); i++) {
                        list2.add(data.get(i).getName());
                        list2Ids.add(data.get(i).getChannelId());

                        // 默认选中类型
                        if (!StUtils.isEmpty(value.getFindString()) && data.get(i).getName().equals(value.getFindString())) {
                            channelPos = i;
                        } else {
                            channelPos = -1;
                        }

                        if (!StUtils.isEmpty(twoIds) && twoIds.equals(data.get(i).getChannelId())) {
                            channelPos = i;
                        }
                    }

                    tgv_two.requestShowMeasure(true);
                    tgv_two.setRadioButtonByFlg(list2, R.layout.tag_screen_radiobutton, 1);
                    if (channelPos >= 0) {
                        tgv_two.checkSelecteDefault(channelPos + 1, 1);

                    } else {
                        tv_two.setBackgroundResource(R.drawable.item_tab_text_true);
                        tv_two.setTextColor(Color.parseColor("#ffffff"));
                    }
                }
            }

            @Override
            public boolean onFail(ChannelListResponse response) {
                return false;
            }
        }, "{\"tab\":\"" + tab + "\"}");

        tgv_two.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId > -1) {
                    twoIds = list2Ids.get(checkedId - 1);
                    tv_two.setBackgroundResource(R.drawable.tag_screen_select_false);
                    tv_two.setTextColor(Color.parseColor("#222222"));
                } else {
                    twoIds = "";
                    tv_two.setBackgroundResource(R.drawable.item_tab_text_true);
                    tv_two.setTextColor(Color.parseColor("#ffffff"));
                }
                MobclickAgent.onEvent(context, UMConstant.STACKS_MORE_SELF_SCREEN_TYPE_CLICK, twoIds);
            }
        });
        tv_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tgv_two.clearCheck();
            }
        });

        List<String> list3 = new ArrayList<>();
        list3.add("30万以下");
        list3.add("30-50万");
        list3.add("50-100万");
        list3.add("100万以上");
        tgv_three.requestShowMeasure(true);
        tgv_three.setRadioButtonByFlg(list3, R.layout.tag_screen_radiobutton, 1);
        if (!StUtils.isEmpty(threeIds)) {
            tgv_three.checkSelecteDefault(Integer.parseInt(threeIds), 1);
            tv_three.setBackgroundResource(R.drawable.tag_screen_select_false);
            tv_three.setTextColor(Color.parseColor("#222222"));
        }
        tgv_three.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId > -1) {
                    threeIds = checkedId + "";
                    tv_three.setBackgroundResource(R.drawable.tag_screen_select_false);
                    tv_three.setTextColor(Color.parseColor("#222222"));
                } else {
                    threeIds = "";
                    tv_three.setBackgroundResource(R.drawable.item_tab_text_true);
                    tv_three.setTextColor(Color.parseColor("#ffffff"));
                }
                MobclickAgent.onEvent(context, UMConstant.STACKS_MORE_SELF_SCREEN_WORD_COUNT_CLICK, threeIds);
            }
        });
        tv_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tgv_three.clearCheck();
            }
        });
        List<String> list4 = new ArrayList<>();
        list4.add("近1年");
        list4.add("近2年");
        list4.add("近3年");
        tgv_four.requestShowMeasure(true);
        tgv_four.setRadioButtonByFlg(list4, R.layout.tag_screen_radiobutton, 1);

        if (!StUtils.isEmpty(fourIds)) {
            if (fourIds.contains("20")) {
                ll_time.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_time.setTextColor(Color.parseColor("#ffffff"));
                tv_four.setBackgroundResource(R.drawable.tag_screen_select_false);
                tv_four.setTextColor(Color.parseColor("#222222"));
                tv_time.setText(fourIds);
            } else {
                tgv_four.checkSelecteDefault(Integer.parseInt(fourIds), 1);
                tv_four.setBackgroundResource(R.drawable.tag_screen_select_false);
                tv_four.setTextColor(Color.parseColor("#222222"));
            }
        }
        ll_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimeGroupPop.selectTimeGroupPop(mView, context, new TimeGroupPop.PopDismissListener() {
                    @Override
                    public void onOkClick(String time) {
                        if (!StUtils.isEmpty(time)) {
                            tv_time.setText(time);
                            fourIds = time;
                            tgv_four.clearCheck();
                            tv_four.setBackgroundResource(R.drawable.tag_screen_select_false);
                            tv_four.setTextColor(Color.parseColor("#222222"));
                            ll_time.setBackgroundResource(R.drawable.item_tab_text_true);
                            tv_time.setTextColor(Color.parseColor("#ffffff"));
                        }

                    }
                });
            }
        });
        tgv_four.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId > -1) {
                    fourIds = checkedId + "";
                    tv_four.setBackgroundResource(R.drawable.tag_screen_select_false);
                    tv_four.setTextColor(Color.parseColor("#222222"));
                    ll_time.setBackgroundResource(R.drawable.tag_screen_select);
                    tv_time.setTextColor(Color.parseColor("#222222"));
                    MobclickAgent.onEvent(context, UMConstant.STACKS_MORE_SELF_SCREEN_RELEASE_TIME_CLICK, fourIds);

                } else {
                    if (!fourIds.contains("20")) {
                        fourIds = "";
                        tv_four.setBackgroundResource(R.drawable.item_tab_text_true);
                        tv_four.setTextColor(Color.parseColor("#ffffff"));
                        ll_time.setBackgroundResource(R.drawable.tag_screen_select);
                        tv_time.setTextColor(Color.parseColor("#222222"));
                    }
                }
            }
        });
        tv_four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fourIds = "";
                tgv_four.clearCheck();
            }
        });

        //设置 popupWindow 里的按钮的事件
        List<String> list5 = new ArrayList<>();
        list5.add("不限");
        list5.add("已读");
        list5.add("未读");
        tgv_five.requestShowMeasure(true);
        tgv_five.setRadioButtonByFlg(list5, R.layout.tag_screen_radiobutton, 0);
        //默认选中
        if (StUtils.isEmpty(fiveIds)) {
            tgv_five.checkSelecteDefault(0, 0);
        } else {
            tgv_five.checkSelecteDefault(Integer.parseInt(fiveIds), 0);
        }
        tgv_five.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == 0) {
                    fiveIds = "";
                } else {
                    fiveIds = checkedId + "";
                }
                MobclickAgent.onEvent(context, UMConstant.STACKS_MORE_SELF_SCREEN_READ_STATUS_CLICK, String.valueOf(tgv_five.getRadioButtonCheck()));
            }
        });

        //设置 popupWindow 里的按钮的事件
        List<String> list6 = new ArrayList<>();
        list6.add("累计热度");
        list6.add("销售量");
        list6.add("完结时间");
        tgv_six.requestShowMeasure(true);
        tgv_six.setRadioButtonByFlg(list6, R.layout.tag_screen_radiobutton, 1);
        //默认选中
        if (StUtils.isEmpty(sixIds)) {
            tgv_six.checkSelecteDefault(1, 1);
        } else {
            tgv_six.checkSelecteDefault(Integer.parseInt(sixIds), 1);
        }
        tgv_six.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                sixIds = checkedId + "";
                MobclickAgent.onEvent(context, UMConstant.STACKS_MORE_SELF_SCREEN_SORT_BY_CLICK, sixIds);
            }
        });
        tv_tab_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tagsId.clear();
                tv_tab_two.setBackgroundResource(R.drawable.tag_screen_select_false);
                tv_tab_two.setTextColor(Color.parseColor("#222222"));
                tv_tab_two.setText("筛选标签");
                tv_tab_one.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_tab_one.setTextColor(Color.parseColor("#ffffff"));
            }
        });
        tv_tab_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TabScreenPop().tabScreenPopWindow(mView, context, tagsId, new TabScreenPop.PopTabScreenListener() {
                    @Override
                    public void onValueClick(List<String> value) {
                        selectTab(value);
                    }
                });
                MobclickAgent.onEvent(context, UMConstant.STACKS_MORE_SELF_SCREEN_LABEL_CLICK);

            }
        });

        tv_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                twoIds = "";
                tgv_two.clearCheck();
                tv_two.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_two.setTextColor(Color.parseColor("#ffffff"));

                threeIds = "";
                tgv_three.clearCheck();
                tv_three.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_three.setTextColor(Color.parseColor("#ffffff"));
                fourIds = "";
                tgv_four.clearCheck();
                tv_four.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_four.setTextColor(Color.parseColor("#ffffff"));

                fiveIds = "";
                tgv_five.checkSelecteDefault(0);

                sixIds = "";
                tgv_six.checkSelecteDefault(0);
                if (tagsId != null && tagsId.size() > 0) {
                    tagsId.clear();
                }
                tv_tab_two.setBackgroundResource(R.drawable.tag_screen_select_false);
                tv_tab_two.setTextColor(Color.parseColor("#222222"));
                tv_tab_two.setText("筛选标签");
                tv_tab_one.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_tab_one.setTextColor(Color.parseColor("#ffffff"));
            }
        });

        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AllStackRequest bookRequest = new AllStackRequest();
                bookRequest.setTab(value.getTab());
                List<String> channel = new ArrayList<>();
                channel.add(twoIds);
                bookRequest.setChannels(channel);
                bookRequest.setWordSize(threeIds);
                bookRequest.setPublishTime(fourIds);
                bookRequest.setTagIds(tagsId);
                bookRequest.setReadingStatus(fiveIds);
                bookRequest.setSortType(sixIds);
                bookRequest.setOffset(0);
                bookRequest.setLimit(10);
                listener.onValueClick(bookRequest);
                popWindow.dismiss();
                MobclickAgent.onEvent(context, UMConstant.STACKS_MORE_SELF_SCREEN_SURE_CLICK);
            }
        });

        // 获取标签列表
        listTag(value);
    }

    /**
     * 标签选中转台
     * @param value
     */
    private void selectTab(List<String> value) {
        if (value == null) {
            tagsId.clear();
            tv_tab_two.setBackgroundResource(R.drawable.tag_screen_select_false);
            tv_tab_two.setTextColor(Color.parseColor("#222222"));
            tv_tab_two.setText("筛选标签");
            tv_tab_one.setBackgroundResource(R.drawable.item_tab_text_true);
            tv_tab_one.setTextColor(Color.parseColor("#ffffff"));
        } else {
            tagsId = value;
            tv_tab_one.setBackgroundResource(R.drawable.tag_screen_select_false);
            tv_tab_one.setTextColor(Color.parseColor("#222222"));
            tv_tab_two.setText("已选" + value.size() + "个");
            tv_tab_two.setBackgroundResource(R.drawable.item_tab_text_true);
            tv_tab_two.setTextColor(Color.parseColor("#ffffff"));
        }
    }

    public interface PopOptionalListener {
        void onValueClick(AllStackRequest value);
    }

    public void setBackgroundAlpha(float alpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);
    }

    private List<TagListResopnse.DataBean> dataBeans;
    /**
     * 获取标签列表
     */
    private void listTag(AllStackRequest value) {
        ((FFActivity) mActivity).post(Url.listTag, null,TagListResopnse .class, new FFNetWorkCallBack<TagListResopnse>() {
            @Override
            public void onSuccess(TagListResopnse response) {
                if (response.getCode() == 200) {
                    dataBeans = response.getData();

                    // 如果标签列表存在，则选中标签列表
                    if (value != null && !TextUtils.isEmpty(value.getFindString())) {
                        if (tagsId == null) {
                            tagsId = new ArrayList<>();
                        }

                        for (int i = 0; i < dataBeans.size(); i++) {
                            if (value.getFindString().equals(dataBeans.get(i).getName()) && !tagsId.contains(dataBeans.get(i).getTagId())) {
                                tagsId.add(dataBeans.get(i).getTagId());
                            }
                        }

                        selectTab(tagsId);
                    } else {
                        selectTab(null);
                    }
                }
            }

            @Override
            public boolean onFail(TagListResopnse response) {
                return false;
            }
        });
    }
}
